var invoicePlugin;// 发票打印控件

/* 初始化发票打印控件 */
function initInvoice() {
	if (!invoicePlugin) {
		invoicePlugin = document.getElementById("invoicePlugin");
	}
}

/*
 * orderno 单据编号 normalType 0 是开正常发票，1是作废发票 openType 1是开具发票 0是不开 printAuto 0
 * 是发票开具后不打印，1是发票开具后立即打印
 */
function printInvoiceOne(orderno, normalType, openType, item) {
	if (item) {
		$(item).html("已打印发票");
		$(item).attr("disabled", "disabled");
	}
	$.ajaxQueue.offer({
		"type" : "post",
		"url" : path + "/v2/invoicePrint/printInvoice",
		"dataType" : "json",
		"data" : {
			orderNo : orderno,
			normalType : normalType,
			openType : openType,
			printAuto : (normalType == '1' && openType == '0') ? '0' :'1'
		},
		"success" : function(data) {
			$.each(data.content, function(i) {
				doPrintInvoice(data.content[i]);
			});
		},
		error : function() {
			alert("服务器异常");
		}
	});
}

/* 打印 */
function doPrintInvoice(xml) {
	showBg();
	initInvoice();
	// var responseXML = test();
	var responseXML = invoicePlugin.GET_INVOICE_DATA1(xml);
	var returnObj = parseReturn(responseXML);
	//正常操作，修改数据
	if (returnObj.returnCode == '0') {
		$.ajax({
			"type" : "post",
			"url" : path + "/v2/invoicePrint/changePrintInvoiceStatus",
			"dataType" : "json",
			"data" : returnObj,
			"success" : function(resp) {
			},
			"error" : function() {
				alert("服务器异常");
			},
			"complete":function(){
				closeBg();
			}
		});
	} else {
		jAlert("单据["+returnObj.orderNO+"]操作发票异常:"+returnObj.msg,"温馨提示");
		closeBg();
	}
}
function parseReturn(xmlText) {
	var xmlObject = $.parseXML(xmlText);
	return {
		"orderNO" : $(xmlObject).find("单据编号").text(),
		"typeCode" : $(xmlObject).find("类别代码").text(),
		"invoiceNo" : $(xmlObject).find("发票号码").text(),
		"invoiceTime" : $(xmlObject).find("开票日期").text(),
		"invoiceOperate" : $(xmlObject).find("开票人").text(),
		"deleteFlg" : $(xmlObject).find("作废标志").text(),
		"returnCode" : $(xmlObject).find("返回结果代码").text(),
		"msg" : $(xmlObject).find("返回结果信息").text(),
	};
}
/* 解析XML */
function loadXML(xmlFile) {
	var xmlDoc;
	if (window.ActiveXObject) {
		xmlDoc = new ActiveXObject('Microsoft.XMLDOM');
		xmlDoc.async = false;
		xmlDoc.load(xmlFile);
	} else if (document.implementation
			&& document.implementation.createDocument) {
		xmlDoc = new DOMParser().parseFromString(xml, "text/xml");
	} else {
		return null;
	}
	return xmlDoc;
}
/*
 * orderno 单据编号
 */
function ajaxInvoiceCount(item, taskCode) {
	$.ajax({
		type : 'post',
		url : path + '/v2/invoicePrint/spliteInvoiceOrder',
		data : {
			taskCode : taskCode,
			size : 50
		},
		dataType : 'json',
		success : function(data) {
			if (data.status == 1) {
				printInvoiceByOrdersNormal(data.content.split(";"), item);
			} else {
				alert(data.content);
			}
		},
		error : function() {

		}
	});
}

function printInvoiceByOrders(pageLists, item, normalType, openType) {
	for ( var i = 0; i < pageLists.length; i++) {
		if ("" == pageLists[i]) {
			continue;
		}
		if (pageLists[i] == "finish") {
			if (item) {
				$(item).html("已打印发票");
				$(item).attr("disabled", "disabled");
			}
			continue;
		}
		printInvoiceOne(pageLists[i], normalType, openType, item);
	}
}
/*
 * 正常开票(内部方法) normalType 0 是开正常发票，1是作废发票 openType 1是开具发票 0是不开
 */
function printInvoiceByOrdersNormal(pageLists, item) {
	printInvoiceByOrders(pageLists, item, '0', '1');
}
/*
 * 发票作废(内部方法)
 */
function cancelInvoiceByOrders(pageLists, item) {
	printInvoiceByOrders(pageLists, item, '1', '0');
}
/*
 * 作废重开(内部方法)
 */
function cancelAndRePrintInvoiceByOrders(pageLists, item) {
	printInvoiceByOrders(pageLists, item, '1', '1');
}
/*
 * 正常开票(对外接口) normalType 0 是开正常发票，1是作废发票 openType 1是开具发票 0是不开
 */
function printInvoiceNormalFace(orderno, item) {
	doPrintInvocieBatch(orderno, "normal", item);
}
/*
 * 发票作废(对外接口)
 */
function cancelInvoiceFace(orderno, item) {
	doPrintInvocieBatch(orderno, "cancel", item);
}
/*
 * 作废重开(对外接口)
 */
function cancelAndRePrintInvoiceFace(orderno, item) {
	doPrintInvocieBatch(orderno, "cancelAndRePrint", item);
}

function doPrintInvocieBatch(orderno, type, item) {
	var orders = [];
	// 如果是数组，提示必须有值
	if (orderno instanceof Array) {
		if (orderno.length == 0) {
			jAlert("请至少选择一条订单.", "温馨提示");
			return;
		}
		$.each(orderno, function(i) {
			orders.push(orderno[i]);
		});
	} else {
		orders.push(orderno);
	}
	orders.push("finish");
	switch (type) {
	case "normal":
		printInvoiceByOrdersNormal(orders, item);
		break;
	case "cancel":
		cancelInvoiceByOrders(orders, item);
		break;
	case "cancelAndRePrint":
		cancelAndRePrintInvoiceByOrders(orders, item);
		break;
	}
}

function test() {
	var xml = "";
	xml += '<?xml version="1.0" encoding="UTF-8"?>';
	xml += '<body>';
	xml += '  <root>';
	xml += '    <row id="1">';
	xml += '      <main>';
	xml += '        <单据编号>PD30002100145723</单据编号>';
	xml += '        <类别代码>144061420293</类别代码>';
	xml += '        <发票号码>00147835</发票号码>';
	xml += '        <开票日期>20141210</开票日期>';
	xml += '        <开票人>仝超</开票人>';
	xml += '        <作废标志>0</作废标志>';
	xml += '        <返回结果代码>0</返回结果代码>';
	xml += '        <返回结果信息></返回结果信息>';
	xml += '      </main>';
	xml += '    </row>';
	xml += '  </root>';
	xml += '</body>';
	return xml;
}
